package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.model.Result;
import com.woniuxy.commons.utils.BeanCopyUtil;
import com.woniuxy.commons.utils.PagesUtils;
import com.woniuxy.controller.form.ProductForm;
import com.woniuxy.dao.ProductsInfoDao;
import com.woniuxy.dao.model.ProductsInfo;
import com.woniuxy.service.ProductsInfoService;
import com.woniuxy.service.dto.GetMyFoodProductsDto;
import com.woniuxy.service.dto.UserDetailsDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 餐品表 前端控制器
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@Api(tags = "餐品信息操作接口,餐品的增删改查")
@RestController
@RequestMapping("/productsInfo")
public class ProductsInfoController {
    @Autowired
    private ProductsInfoService productsInfoService;
    @Resource
    private  ProductsInfoDao productsInfoDao;

    /**
     * 餐品新增
     * @param productForm
     * @return
     */
    @ApiOperation("餐品新增")
    @PostMapping("add")
    public Result addProduct(@RequestBody ProductForm productForm) {
        return Result.success(200, "餐品新增成功",
                productsInfoService.addProduct(productForm));
    }

    /**
     * 根据id,删除餐品
     * @param id
     * @return
     */
    @ApiOperation("根据id,删除餐品")
    @ApiImplicitParam(name = "id", value = "餐品id")
    @DeleteMapping("del/{id}")
    public Result delProduct(@PathVariable String id) {
        if (id.equals("") || id.equals("undefined"))
            return Result.fail(500, "餐品id不存在");
        if (productsInfoService.delProductById(Long.parseLong(id)))
            return Result.success(200, "商品删除成功", null);
        return Result.fail(500, "餐品删除失败");
    }

    /**
     * 餐品信息修改
     * @param productForm
     * @return
     */
    @ApiOperation("餐品信息修改")
    @PutMapping("update")
    public Result updateProduct(@RequestBody ProductForm productForm) {
        return Result.success(200, "修改成功",
                productsInfoService.updateProduct(productForm));
    }

    /**
     * 根据商家id,查询餐品信息
     * @param storeId
     * @return
     */
    @ApiOperation("根据商家id,查询餐品信息")
    @ApiImplicitParam(name = "storeId", value = "商家id")
    @GetMapping("list/{storeId}")
    public Result getProducts(@PathVariable String storeId) {
        if (storeId.equals("") || storeId.equals("undefined"))
            return Result.fail(500, "该商家不存在");
        return Result.success(200, "",
                productsInfoService.getProductsByStoreId(Long.parseLong(storeId)));
    }

    /**
     * 首页餐品信息查询(4条)
     * @return
     */
    @ApiOperation("首页餐品信息查询(4条)")
    @GetMapping("listShow")
    public Result getProduct() {
        return Result.success(200, "餐品信息查询成功",
                productsInfoDao.showProductsLimit4());
    }

    /**
     * 首页餐品信息查询
     * @return
     */
    @ApiOperation("首页餐品信息查询")
    @GetMapping("listShow2")
    public Result getProduct2() {
        return Result.success(200, "餐品信息查询成功",
                productsInfoService.list());
    }

    /**
     * 后台查询自己的菜品的所有种类
     * @return
     */
    @ApiOperation("后台查询自己的菜品的所有种类")
    @GetMapping("/getMyFoodProducts")
    public Result getMyFoodProducts() {

        UserDetailsDto userDetailsDto = (UserDetailsDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        Long storeId = userDetailsDto.getId();

        List<ProductsInfo> productsInfoList = productsInfoService.list(new QueryWrapper<ProductsInfo>().eq("storeId", storeId));

        List<GetMyFoodProductsDto> getMyFoodProductsDtos = new ArrayList<>();

        for (ProductsInfo p : productsInfoList) {
            GetMyFoodProductsDto getMyFoodProductsDto = new GetMyFoodProductsDto();
            getMyFoodProductsDto.setValue(p.getId());
            getMyFoodProductsDto.setLabel(p.getProductsName());
            getMyFoodProductsDtos.add(getMyFoodProductsDto);
        }
        return Result.success(200, "查询成功", getMyFoodProductsDtos);
    }
}

